package com.cn.common.utils.http;

import com.cn.common.utils.text.Convert;
import com.cn.common.utils.text.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ServletUtil {
    private static final Logger logger = LoggerFactory.getLogger(ServletUtil.class);
    private static final String[] agent = new String[]{"Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser"};

    public ServletUtil() {
    }

    public static String getParameter(String name) {
        return getRequest().getParameter(name);
    }

    public static String getParameter(String name, String defaultValue) {
        return Convert.toStr(getRequest().getParameter(name), defaultValue);
    }

    public static Integer getParameterToInt(String name) {
        return Convert.toInt(getRequest().getParameter(name));
    }

    public static Integer getParameterToInt(String name, Integer defaultValue) {
        return Convert.toInt(getRequest().getParameter(name), defaultValue);
    }

    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }

    public static HttpServletResponse getResponse() {
        return getRequestAttributes().getResponse();
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes)attributes;
    }

    public static String renderString(HttpServletResponse response, String string) {
        try {
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        } catch (IOException var3) {
            logger.error(var3.getMessage());
        }

        return null;
    }

    public static boolean isAjaxRequest(HttpServletRequest request) {
        String accept = request.getHeader("accept");
        if (accept != null && accept.indexOf("application/json") != -1) {
            return true;
        } else {
            String xRequestedWith = request.getHeader("X-Requested-With");
            if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1) {
                return true;
            } else {
                String uri = request.getRequestURI();
                if (StringUtil.inStringIgnoreCase(uri, new String[]{".json", ".xml"})) {
                    return true;
                } else {
                    String ajax = request.getParameter("__ajax");
                    return StringUtil.inStringIgnoreCase(ajax, new String[]{"json", "xml"});
                }
            }
        }
    }

    public static boolean checkAgentIsMobile(String ua) {
        boolean flag = false;
        if ((!ua.contains("Windows NT") || ua.contains("Windows NT") && ua.contains("compatible; MSIE 9.0;")) && !ua.contains("Windows NT") && !ua.contains("Macintosh")) {
            String[] var2 = agent;
            int var3 = var2.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String item = var2[var4];
                if (ua.contains(item)) {
                    flag = true;
                    break;
                }
            }
        }

        return flag;
    }
}